/**
 * 
 */
package com.shelpe.services.sinbad.configuration;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;


import redis.clients.jedis.JedisPoolConfig;

/**
 * redis配置
 * @author louxinhua
 *
 */
@Configuration
@EnableConfigurationProperties(RedisConfigProperties.class)
public class RedisConfig {

	private static Logger logger = LoggerFactory.getLogger(RedisConfig.class);
	
	@Autowired
	private RedisConfigProperties redisConfigProperties;
	
	public JedisPoolConfig getRedisConfig(){  
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxIdle(redisConfigProperties.getPoolMaxIdle());
        config.setMinIdle(redisConfigProperties.getPoolMinIdle());
        config.setMaxTotal(redisConfigProperties.getPoolMaxActive());
        config.setMaxWaitMillis(redisConfigProperties.getPoolMaxWait());
        return config;  
    }
	
	@Bean 
    public JedisConnectionFactory getConnectionFactory(){  
        JedisConnectionFactory factory = new JedisConnectionFactory();  
        factory.setHostName(redisConfigProperties.getHost());
        factory.setPort(redisConfigProperties.getPort());
        factory.setDatabase(redisConfigProperties.getDatabase());
        factory.setPassword(redisConfigProperties.getPassword());
        
        JedisPoolConfig config = getRedisConfig();  
        factory.setPoolConfig(config);  
        
        logger.info("========== JedisConnectionFactory bean init success. =============");  
        return factory;  
    } 
	
	
	@Bean  
    public RedisTemplate<String, Object> getRedisTemplate(){  
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(getConnectionFactory());
        return template;  
    }  
	
	
}
